<?php
namespace app\reduction\controller;
use app\ApiRest;
use think\App;
use app\reduction\model\Active as Model;
use app\reduction\model\Company;
use app\reduction\model\UserRecord;

use think\facade\Cache;
use think\facade\Db;

class IndexActive extends ApiRest
{

    protected $model;

    protected $company_model;

    protected $user_record_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model         = new Model();

        $this->company_model = new Company();

        $this->user_record_model = new UserRecord();

        //初始化一下
//        $this->user_record_model->initRecord();
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 10:08
     * @功能说明:支付页面信息
     */
    public function payPageInfo(){

        $input = $this->_input;
        //能使用的优惠券数量
        $data['coupon_num'] = $this->user_record_model->canUseCount($this->getUserId());
        //员工头像
        $data['staff_img']  = lbUserAvatar($input['staff_id']);
        //员工名称
        $data['staff_name'] = lbUserName($input['staff_id']);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 10:53
     * @功能说明:满减详情
     */
    public function couponInfo(){

        $input = $this->_param;
        //券的详情
        $data['coupon_info'] = $this->model->activeInfo(['id'=>$input['active_id']]);
        //员工头像
        $data['staff_img']   = lbUserAvatar($input['staff_id']);
        //查看是否领取完
        if(!empty($data['coupon_info'])){
            //领取完的状态为-1
            if($data['coupon_info']['num']<=$data['coupon_info']['get_num']){

                $data['coupon_info']['status'] = -1;
            }
        }
        //判断用户是否领取过
        $dis = [

            'user_id'   => $this->getUserId(),

            'staff_id'  => $input['staff_id'],

            'active_id' => $input['active_id'],
            //未使用的
            'status'    => 1
        ];

        $have = $this->user_record_model->recordInfo($dis);

        $data['have_get']  = !empty($have)?1:0;

        $data['record_id'] = !empty($have)?$have['id']:0;

        return $this->success($data);
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-22 11:29
     * @功能说明:
     */
    public function staffCouponList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //
        $diss[] = ['c.fans_id','=',$this->getUserId()];

        $diss[] = ['a.use_condition','=',0];

        $data  = $this->model->staffActiveList($dis,$diss);

        if(!empty($data['data'])){

            $user_record = new UserRecord();

            foreach ($data['data'] as &$v){

                $dis = [

                    'active_id' => $v['id'],

                    'staff_id'  => $this->getUserId()
                ];
                //该员工下面的领取数量
                $v['staff_get_num'] = $user_record->recordCount($dis);
                //领取完的状态为2
                if($v['num']<=$v['get_num']){

                    $v['status'] = -1;
                }

                $v['staff_img'] = lbUserAvatar($this->getUserId());

                $v['staff_id']  = $this->getUserId();
            }
        }

        return $this->success($data);
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 16:56
     * @功能说明:满减支付码
     */

    public function reductionQr(){

        $input = $this->_input;

        $qr    = getCache($input['type'].'-'.$input['staff_id'].'-'.$input['id']."-reductionqr",$this->_uniacid);
        if(empty($qr)){

            $data = longbingCreateWxCode($this->_uniacid,$input,$input['page']);

            $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);

            $qr   = $data['qr_path'];

            setCache($input['type'].'-'.$input['staff_id'].'-'.$input['id']."-reductionqr",$this->_uniacid,$qr,3600,$this->_uniacid);
        }
        $data['qr'] = $qr;

        return $this->success($data);
    }




}
